package com.j2bugzilla.rpc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.j2bugzilla.base.BugzillaMethod;
import com.j2bugzilla.base.UserInfo;

public class GetUserInfo implements BugzillaMethod {

	private static final String METHOD_NAME = "User.get";
	
	private Map<Object, Object> params = new HashMap<Object, Object>();

	private Map<Object, Object> hash = Collections.emptyMap();
	
	public List<UserInfo> getUserInfo() {
		Object products = hash.get("users");
		if(products == null) { return null; }

		Object[] arr = (Object[])products;
		if(arr.length == 0) { return null; }
		
		List<UserInfo> res = new ArrayList<UserInfo>();

		for (Object p : arr) {
			@SuppressWarnings("unchecked")//Cast to form specified by webservice
			Map<Object, Object> userMap = (Map<Object, Object>)p;
			UserInfo userInfo = new UserInfo(
					(Integer)userMap.get("id"),
					(String)userMap.get("real_name"),
					(String)userMap.get("email"),
					(String)userMap.get("name"),
					(Object[])userMap.get("groups"));
			
			res.add(userInfo);
		}
		return res;
	}
	public GetUserInfo(String name) {
		this(Collections.singletonList(name));
	}

	public GetUserInfo(List<String> names) {
		this.params.put("names", names);
	}

	@Override
	public void setResultMap(Map<Object, Object> hash) {
		this.hash = hash;
	}

	@Override
	public Map<Object, Object> getParameterMap() {
		return Collections.unmodifiableMap(params);
	}

	@Override
	public String getMethodName() {
		return METHOD_NAME;
	}

}
